package com.e3.sysmg.security.client;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.e3.sysmg.security.model.RoleModel;
import com.e3.sysmg.security.model.RoleQC;
import com.e3.web.core.client.GXTHelper;
import com.e3.web.core.client.GwtAsyncCallback;
import com.e3.web.core.client.ListenerManager;
import com.e3.web.core.client.QueryListener;
import com.e3.web.widget.client.MyDialog;
import com.e3.web.widget.client.MyField;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.google.gwt.user.client.Element;

public class RoleQueryDialog extends MyDialog {
	private RoleRPCServiceAsync service = RoleRPCService.Util.getInstance();
	
	private static RoleMessages MESSAGES = RoleMessages.INSTANCE;
	
	private FormPanel formPanel = new FormPanel();
	
	private List<Field<?>> fields;
	
	private FormData formData = new FormData("100%");
	
	private Map<String,Field<?>> formFieldsMap = new HashMap<String, Field<?>>();
	
	private RoleQueryDialog queryUI = this;
	
	private ListenerManager<RoleModel> listenerManager = new ListenerManager<RoleModel>();
	
	@Override
	protected void onRender(Element parent, int pos) {
		super.onRender(parent, pos);
		this.setSize(400, 300);
		this.setLayout(new FitLayout());
		this.setHeading(MESSAGES.title_query(MESSAGES.local_name()));
		
		formPanel.setHeaderVisible(false);
		this.add(formPanel);
		
		initFormPanel();
		
	}

	private void initFormPanel() {
		if(getQueryFields() == null){
			return;
		}
		fields = getQueryFields();
		for(int i=0;i<fields.size();i++){
			Field<?> field = fields.get(i);
			formPanel.add(field, formData);
			formFieldsMap.put(field.getName(), field);
		}
	}
	
	public void addQueryListener(QueryListener<RoleModel> listener){
		listenerManager.addQueryListener(listener);
	}

	protected void onOk() {
		MyField field = (MyField)formFieldsMap.get(RoleModel.CODE);
		String code = (String)field.getFieldValue();
		field = (MyField)formFieldsMap.get(RoleModel.NAME);
		String name = (String)field.getFieldValue();
		
		final RoleQC qc = new RoleQC();
		qc.setCode(code);
		qc.setName(name);
		GXTHelper.maskQuery(this);
		service.getRolesByCondition(qc, new GwtAsyncCallback<List<RoleModel>>(this) {
			@Override
			public void doSuccess(List<RoleModel> result) {
				listenerManager.notifyQuerySuccess(qc,result);
				queryUI.setVisible(false);
			}
			
		});
	
	}
	
	private List<Field<?>> getQueryFields(){
		if(fields!=null){
			return fields;
		}
		String[] names = {RoleModel.CODE,RoleModel.NAME};
		List<Field<?>> fields = new ArrayList<Field<?>>();
		
		for (String name : names) {
			Field<?> formField = RoleHelper.INSTANCE.createFormField(name);
			fields.add(formField);
			formFieldsMap.put(name, formField);
		}
		return fields;
	}

}
